#include "processwin.h"
#include "opencvhelper.h"
#include "qmenu.h"
#include "qstackedwidget.h"
#include "ui_processwin.h"
#include "glimagewidget.h"
#include<debugx>

void exeProcess(QListWidgetItem *item);
QToolButton * createButton(ProcessWin *win,QString text)
{
    auto btnTmp = new QToolButton();
    btnTmp->setGeometry(QRect(100, 30, 24, 21));
    btnTmp->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    btnTmp->setText(text);
    win->ui->horizontalLayout->addWidget(btnTmp,1);
    return btnTmp;
}
void createFuncButton(QStackedWidget *stackedWidget);
ProcessWin::ProcessWin(Plugin *p, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ProcessWin)
{
    ui->setupUi(this);
    plugin=p;
    //setWindowTitle("Process");
    setContentsMargins(0,0,0,0);
 //   setMaximumWidth(900);
   splitter=new QSplitter(Qt::Vertical);

   imageWin=new GLImageWidget(plugin);
    splitter->addWidget(imageWin);
   resultWin=new GLImageWidget(plugin);
   splitter->addWidget(resultWin);

   ui->verticalLayout_2->addWidget(splitter,10);

   connect(ui->btnExe,  &QAbstractButton::clicked, ui->btnExe, [=]() {
       void exeProcess();
       exeProcess();
   });
   connect(ui->btnSave,  &QAbstractButton::clicked, this, &ProcessWin::slot_btnClicked );
   connect(ui->btnClear,  &QAbstractButton::clicked, ui->btnClear, [=]() {
      ui->lvSelected->clear();
   });

   auto select=createButton(this, "Selection");
   connect(select,  &QAbstractButton::clicked, select, [=]() {
       auto img=imageWin->getSeletedImage();
       imageWin->setImage(img);
       return;
   });
   auto btnClip = createButton(this, "Clipboard");
   connect(btnClip,  &QAbstractButton::clicked, btnClip, [=]() {
       cv::Mat matClipboard;

       if(matClipboard.empty())
        {
           auto img=imageWin->getSeletedImage();
           if(img.isNull())
               img=imageWin->getImage();
           if(img.isNull())return;
           matClipboard=QImageToMat(img);
       }
       cv::namedWindow("Clipboard",0);
       cv::imshow("Clipboard",matClipboard);
   });

   auto btnTmp = createButton(this, "Switch");

   auto stackedWidget = new QStackedWidget();
    stackedWidget->resize(480, 100);
    stackedWidget->setStyleSheet("QStackedWidget {  background-color: white;  border: 0px solid transparent;}");
    ui->horizontalLayout->addWidget(stackedWidget);
 //  createFuncButton(stackedWidget);

    QEventLoop::connect(btnTmp,  &QAbstractButton::clicked, btnTmp, [=]() {
        auto index= stackedWidget->currentIndex();
        index++;
        if(stackedWidget->count()==index)index=0;
        stackedWidget->setCurrentIndex(index);
    });
   ui->lvFun->setContextMenuPolicy(Qt::CustomContextMenu);
   ui->lvSelected->setContextMenuPolicy(Qt::CustomContextMenu);
   // 创建 menu
   QMenu* popMenu = new QMenu(this);
   QAction* action = new QAction("exec", this);
   popMenu->addAction(action);
   connect(action, &QAction::triggered, action, [this](){
       auto currentItem= ui->lvSelected->currentItem();
       if(currentItem)
           {
           exeProcess(currentItem);
           return;
       }
       currentItem= ui->lvFun->currentItem();
       if(currentItem)
       {
           exeProcess(currentItem);
           return;
       }

   });

   // 关联自定义的菜单的信号槽
   connect(ui->lvFun, &QListWidget::customContextMenuRequested, this, [popMenu] (const QPoint &)
           { popMenu->exec(QCursor::pos()); });
   connect(ui->lvSelected, &QListWidget::customContextMenuRequested, this, [popMenu] (const QPoint &)
           { popMenu->exec(QCursor::pos()); });

   connect(ui->lvFun,  &QListWidget::doubleClicked, this, &ProcessWin::fun_doubleClicked );
   connect(ui->lvFun,  &QListWidget::currentRowChanged, this, &ProcessWin::fun_currentRowChanged );
    connect(ui->lvFun,  &QListWidget::itemClicked, this, &ProcessWin::fun_itemClicked );

   connect(ui->lvSelected,  &QListWidget::doubleClicked, this, &ProcessWin::selected_doubleClicked );
   connect(ui->lvSelected,  &QListWidget::currentRowChanged, this, &ProcessWin::selected_currentRowChanged );
   connect(ui->lvSelected,  &QListWidget::itemClicked, this, &ProcessWin::selected_itemClicked );


   connect(ui->btnArg,  &QAbstractButton::clicked, this,  [this](){
       auto currentItem= ui->lvSelected->currentItem();
       if(!currentItem)return;
       currentItem->setData(Qt::UserRole+1,ui->txtArg->toPlainText());
        } );

}

ProcessWin::~ProcessWin()
{
    delete ui;
}
bool firstResize=true;
void ProcessWin::resizeEvent(QResizeEvent *)
{
    if(!firstResize || windowState()==Qt::WindowFullScreen)
    setMaximumWidth(3200);
    firstResize=false;
}
extern ProcessWin *processWin;
QRect processWinGetSelection()
{
   QRect ajust(0, 0, 0, 0);
   if (!processWin)
     return ajust;
   return processWin->imageWin->getSeleted();
}
cv::Rect processWinGetSelectionRect()
{
    auto qr=processWinGetSelection();
    auto topLeft=qr.topLeft();
    auto bottomRight=qr.bottomRight();

    int x=topLeft.x();
    int y=topLeft.y();
    int x1=bottomRight.x();
    int y1=bottomRight.y();

    if(x<0)x=0;
    if(y<0)y=0;
     cv::Rect area_keep = cv::Rect(x, y, x1-x, y1 - y);
    return area_keep;
}

extern QHash<QString,ProcessFun *> processFunMap;
void ProcessWin::slot_btnClicked()
{
    auto btn = qobject_cast<QAbstractButton*>(sender());

    if(!btn)return;
    auto text=btn->text();
    debugx2(text);

    if(text=="保存")
    {

        return;
    }



}
void clear(Ui::ProcessWin *ui)
{
    ui->txtArg->clear();
    ui->txtDescription->clear();
    ui->txtDefault->clear();
}

bool currentRowChanged=false;
void ProcessWin::selected_currentRowChanged(int currentRow)
{
    debugx();
    if(currentRow==-1)return;

    auto item =ui->lvSelected->item(currentRow);
    if(!item)return;
    currentRowChanged = false;
    selected_itemClicked(item);
    currentRowChanged=true;
}
void ProcessWin::selected_doubleClicked(const QModelIndex &)
{
     debugx();
    auto currentItem= ui->lvSelected->currentItem();
    if(!currentItem)return;
    ui->lvSelected->removeItemWidget(currentItem);
    delete currentItem;
}
void ProcessWin::selected_itemClicked(QListWidgetItem *item)
{
     debugx();
    if (currentRowChanged) {
        currentRowChanged = false;
        return;
    }
    clear(ui);
    ui->txtDescription->setText(item->toolTip());
    ui->txtDefault->setText(item->data(Qt::UserRole).toString());
    ui->txtArg->setPlainText(item->data(Qt::UserRole+1).toString());
    ui->lvFun->setCurrentRow(-1);


    currentRowChanged=false;
}

void ProcessWin::fun_currentRowChanged(int currentRow)
{
    debugx();
    if(currentRow==-1)return;

    auto item =ui->lvFun->item(currentRow);
    if(!item)return;
    currentRowChanged = false;
    fun_itemClicked(item);
    currentRowChanged=true;
}
void ProcessWin::fun_itemClicked(QListWidgetItem *item)
{
    debugx();
    if (currentRowChanged) {
        currentRowChanged = false;
        return;
    }
    debugx2("do");
    clear(ui);
    ui->txtDescription->setText(item->toolTip());
    ui->txtDefault->setText(item->data(Qt::UserRole).toString());
    ui->txtArg->setPlainText(item->data(Qt::UserRole+1).toString());
    ui->lvSelected->setCurrentRow(-1);

    currentRowChanged=false;
}
void ProcessWin::fun_doubleClicked(const QModelIndex &index)
{
     debugx();
    auto txt=index.data().toString();
    auto funItem=ui->lvFun->itemFromIndex(index);
    auto item=new QListWidgetItem(txt);
    item->setToolTip(funItem->toolTip());
    item->setData(Qt::UserRole,funItem->data(Qt::UserRole));//缺省
    item->setData(Qt::UserRole+1,funItem->data(Qt::UserRole));//当前
    ui->lvSelected->addItem(item);
    clear(ui);
    ui->txtDescription->setText(item->toolTip());
    ui->txtArg->setPlainText(item->data(Qt::UserRole).toString());

}


void ProcessWin::txtArg_textChanged()
{


}








